package nio.muban;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;

public class MySocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8088);
        int recvMsgSize = 0;
        byte[] recvBuf = new byte[1024];
        while(true){
            Socket clntSocket = serverSocket.accept();
            SocketAddress clientAddress = clntSocket.getRemoteSocketAddress();
            System.out.println("Handling client at "+clientAddress);
            InputStream in = clntSocket.getInputStream();
            while((recvMsgSize=in.read(recvBuf))!=-1){
                byte[] temp = new byte[recvMsgSize];
                System.arraycopy(recvBuf, 0, temp, 0, recvMsgSize);
                System.out.println(new String(temp));
            }
        }


    }
}
